<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入设备编号"
        v-model="queryForm.deviceCode"
      >
      </el-input>
      <el-select
        clearable
        style="width: 150px"
        class="filter-item"
        v-model="queryForm.logType"
        placeholder="请选择操作类型"
      >
        <el-option
          v-for="(val, key) in typeOptions"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-date-picker
        class="filter-item"
        type="date"
        v-model="queryForm.startTime"
        :picker-options="pickerStartTime"
        placeholder="选择开始日期"
        style="width: 150px;"
        value-format="yyyy-MM-dd"
        @change="changeTime"
      ></el-date-picker>
      <label class="filter-item">-</label>
      <el-date-picker
        class="filter-item"
        type="date"
        v-model="queryForm.endTime"
        :picker-options="pickerEndTime"
        placeholder="选择结束日期"
        style="width: 150px;"
        value-format="yyyy-MM-dd"
      ></el-date-picker>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
    </div>
    <el-table v-loading="loading" :data="records" style="width: 100%">
      <el-table-column prop="rowId" label="ID" width="300" align="center">
      </el-table-column>
      <el-table-column
        prop="deviceCode"
        label="设备编号"
        width="300"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="logType"
        label="设备状态"
        width="300"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.LOG_TYPE[scope.row.logType] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="300"
        align="center"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding"
        width="300"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'deviceOnlineLog:delete'"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.rowId)"
            title="删除"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  queryDeviceOnlineOfflineLog,
  deleteDeviceOnlineOfflineLog
} from '@/api/log'

export default {
  name: 'deviceOnlineOfflineLog',
  data() {
    return {
      loading: false,
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        startTime: '',
        endTime: '',
        logType: undefined,
        title: undefined
      },
      pickerStartTime: {
        disabledDate: time => {
          if (this.queryForm.endTime !== '') {
            return (
              time.getTime() > Date.now() ||
              time.getTime() > this.queryForm.endTime
            )
          } else {
            return time.getTime() > Date.now()
          }
        }
      },
      pickerEndTime: {
        disabledDate: time => {
          return time.getTime() < new Date(this.queryForm.startTime).getTime()
        }
      },
      typeOptions: Dicts.LOG_TYPE,
      records: []
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    changeTime(date) {
      this.queryForm.endTime = ''
      this.pickerEndTime = {
        disabledDate(time) {
          return time.getTime() < new Date(date).getTime()
        }
      }
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryDeviceOnlineOfflineLog(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    handleDelete(rowId) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDeviceOnlineOfflineLog(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
